

let charaters = {
    bd : {
        name: "lv90北斗", 
        base: 225,
        dmgRate: 24,
        criticalRate: 5,
        criticalStrike: 50
    }
}

let weapons = {
    qy: {
        name:"lv90千岩大剑",  
        base: 510,
        criticalRate: 24,
        attRate:41.3 + 56
    },
    lm: {
        name:"lv90狼末(触发buff)",  
        base: 608,
        attRate:49.6 + 40 + 80
    },
    cg: {
        name:"lv90骨剑",  
        base: 510,
        criticalRate: 27.6,
        dmgRate:50
    }
}
let normal_clips = calc_clips_normal();
let normal_clips2 = calc_clips_normal(2);
let charactersDmg = []

Object.values(weapons).forEach(weapon => {
    charactersDmg.push(getDMG([charaters.bd,weapon,{fixedAtt:311,criticalRate:31.1,dmgRate:46.6,attRate:46.6},normal_clips], (data,second) => {
        // 雷套 + 宗室 20大招增伤
        // 15增伤 
        data.dmgRate += 15;
        let attack = data.base *(1 + data.attRate/100) + data.fixedAtt
        let edmg = getDmgDps(data, 2.19 + 2.88*2) * q(-5) * df(90,90); // e
        let qd1 = getDmgDps({...data, dmgRate:data.dmgRate + 20}, 2.19) * q(-5) * df(90,90); // q
        let qd = getDmgDps({...data, dmgRate:data.dmgRate + 20}, 1.73) * q(-5) * df(90,90); // q
        let ret = edmg * 2 + qd1 + qd * 13
        return {point:ret, data, second, attack ,edmg,qd}
    }))
})

console.log(charactersDmg.sort((a,b)=>b.point-a.point));


function getDMG(dataarr, getRet){
    let chance = 25;
    let _ret = [];
    for (let index = 0; index <= chance; index++) {
        let criticalStrike = index; 
        for (let index2 = 0; index2 <= chance - index; index2++) {
            let criticalRate = index2;
            for (let index3 = 0; index3 <= chance-index-index2; index3++) {
                let attRate = index3;
                let defenceRate = chance - index - index2 - index3;
                _ret.push({
                    defenceRate: 7.3 * defenceRate,
                    attRate:5.8 * attRate,
                    criticalRate:3.9 * criticalRate,
                    criticalStrike:7.8 * criticalStrike
                });
            }
        }
    }
    _ret = _ret.map(second => {
        let panel = getPanel([...dataarr, second]);
        return  getRet(panel, second)
    })
    _ret.sort((b,a)=> a.point-b.point)
    return _ret[0]
}

function getDmgDps(data, rate=1){
    let criticalRate = data.criticalRate
    if(criticalRate > 100){criticalRate = 100}
    let baseat = data.base * (1 + data.attRate/100) + data.fixedAtt;
    let bdmg = baseat * (1 + data.criticalStrike/100);
    let bdps = (criticalRate * 10 * bdmg + (1000-criticalRate * 10)*baseat)/1000
    let adddmg = bdps * (1 + data.dmgRate/100)
    return adddmg * rate
}

function getPanel(arr){
    let info = {name1:"",name2:""};
    arr.forEach((item, i)=>{
        if(item.name){
            info["name" + (i+1)] = item.name
        }
        for (const key in item) {
            if(key !== "name"){
                if(info[key]){
                    info[key] += item[key]
                } else {
                    info[key] = item[key]
                }
            }
        }
    })
    return info
}

function q (resistance) {
    if(resistance < 0) {
        return 1 - resistance/200
    } else if (resistance >=0 && resistance < 75){
        return 1 - resistance/100
    } else {
        return 1/(1+resistance/100*4)
    }
}

function df(youLv=0, eLv=0, td){
    return (100+youLv)/(100+youLv+(100+eLv)*(td? (100-td)/100 : 1))
}

// 固定攻击力 19  防御力23 生命值299 5.3% 
function calc_clips_normal (n) {
    let clips = [
        {
            fixedAtt: 19,
            attRate: 5.8,
            criticalRate: 3.9,
            criticalStrike:7.8
        },
        {
            attRate: 5.8,
            criticalRate: 3.9,
            criticalStrike:7.8
        },
        {
            fixedAtt: 19,
            criticalRate: 3.9,
            criticalStrike:7.8
        },
        {
            fixedAtt: 19,
            attRate: 5.8,
            criticalRate: 3.9,
            criticalStrike:7.8
        },
        {
            fixedAtt: 19,
            attRate: 5.8,
            criticalRate:n!=2? 0:3.9,
            criticalStrike:n!=2? 7.8:0
        }
    ]
    let data = {}
    clips.forEach(item => {
        for (const key in item) {
            data[key] = data[key]? (data[key]+item[key]) : item[key]
        }
    })
    return data
}

